| Conditions | 6 |
| Total Lines | 56 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import { Inject } from '@nestjs/common'; |
||
| 13 | |||
| 14 | public async create(items: FairCalendarView[]): Promise<ICalendarOverview> { |
||
| 15 | const cooperative = await this.cooperativeRepository.find(); |
||
| 16 | if (!cooperative) { |
||
| 17 | throw new CooperativeNotFoundException(); |
||
| 18 | } |
||
| 19 | |||
| 20 | const overviewInDays: ICalendarOverview = { |
||
| 21 | mission: { |
||
| 22 | days: 0, |
||
| 23 | details: [] |
||
| 24 | }, |
||
| 25 | dojo: { |
||
| 26 | days: 0 |
||
| 27 | }, |
||
| 28 | formationConference: { |
||
| 29 | days: 0 |
||
| 30 | }, |
||
| 31 | leave: { |
||
| 32 | days: 0 |
||
| 33 | }, |
||
| 34 | support: { |
||
| 35 | days: 0 |
||
| 36 | }, |
||
| 37 | other: { |
||
| 38 | days: 0 |
||
| 39 | } |
||
| 40 | }; |
||
| 41 | |||
| 42 | for (const { time, type: itemType, project } of items) { |
||
| 43 | const type = itemType.startsWith('leave_') ? 'leave' : itemType; |
||
| 44 | const days = time / cooperative.getDayDuration(); |
||
| 45 | |||
| 46 | overviewInDays[type].days = |
||
| 47 | Math.round((overviewInDays[type].days + days) * 100) / 100; |
||
| 48 | |||
| 49 | if (type === EventType.MISSION) { |
||
| 50 | const missionDetail = overviewInDays[type].details.find( |
||
| 51 | ({ label }) => label === project.name |
||
| 52 | ); |
||
| 53 | |||
| 54 | if (missionDetail) { |
||
| 55 | missionDetail.days = |
||
| 56 | Math.round( |
||
| 57 | (missionDetail.days + Math.round(days * 100) / 100) * 100 |
||
| 58 | ) / 100; |
||
| 59 | } else { |
||
| 60 | overviewInDays[type].details.push({ |
||
| 61 | days, |
||
| 62 | label: project.name |
||
| 63 | }); |
||
| 64 | } |
||
| 65 | } |
||
| 66 | } |
||
| 67 | |||
| 68 | return overviewInDays; |
||
| 69 | } |
||
| 71 |